<script type="text/javascript">
$('form').submit(function() {
    if(!$('form').validationEngine({returnIsValid:true})===true){
        $("#body").load($(this).attr("action"),$(this).serialize(),function(){$('#example').dataTable()});
    }
    else{
        validateEmail(1);
        return true;
    }
  
  return false;
});
</script>
<form action="index.php?page=CMS&action=saveSubsection" method="post">
    <table>
        <tr>
            <td>
                Subsection name
            </td>
            <td>
                <input type="text" name="name"  id="name" class="validate[required,length[0,50]]" <?php if(isset($subsection)) echo "value ='".$subsection->name."' ";?>/>
                <?php if(isset($subsection)) echo "<input type='hidden' name='id' id='id' value='{$subsection->id}'";?>
            </td>
        </tr>
        <tr>
            <td>
                Subsection Type
            </td>
            <td>
                <select name="type">
                    <?php foreach($subsectionTypes as $type){?>
                        <option value="<?php echo $type->id?>" <?php if(isset($subsection)) if($subsection->subsectionTypeId == $type->id) echo "selected";?>><?php echo $type->name?></option>
                    <?php }?>
                </select>
            </td>
        </tr>
        <tr>
            <td>
                Section
            </td>
            <td>
                <select name="section">
                    <?php foreach($sections as $section){?>
                        <option value="<?php echo $section->id?>" <?php if(isset($subsection)) {if($subsection->sectionId == $section->id) echo "selected";}?> > <?php echo $section->name?></option>
                    <?php }?>
                </select>
            </td>
        </tr>
        <tr>
            <td>
                Subsection Status
            </td>
            <td>
                <input type="radio" name="status" value="0" <?php if(isset($subsection)) {if($subsection->status_ == 0) echo "checked";} else echo "checked"; ?>/> closed &nbsp;&nbsp
                <input type="radio" name="status" value="1" <?php if(isset($subsection)) {if($subsection->status_ == 1) echo "checked";} ?> /> open
            </td>
        </tr>
        <tr>
            <td>
                Subsection Order
            </td>
            <td>
                <input type="text" name="order" id="order" class="validate[required,custom[integer]] text-input" <?php if(isset($subsection)) echo "value='".$subsection->order_."'"; else echo "value='0'";?>/>
            </td>
        </tr>

        <?php
            if(isset($subsection)){
                if(count($subsection->content)>0)
                {
                    echo "<tr>";

                    echo "<td>";
                        echo "Main content";
                    echo "</td>";

                    echo "<td>";
                        echo "<select name='mainContent'>";
                        echo "<option value='0'>None</option>";
                        foreach($subsection->contents as $content){
                            $selected = "";
                            if($content->id == $subsection->mainContentId)
                            $selected="selected";
                            echo "<option value='".$content->id."' {$selected}>".$content->name."</option>";
                        }
                        echo "<select/>";
                    echo "</td>";

                    echo "</tr>";
                }
            }
        ?>
        <tr>
            <td>
                <input type="submit" value="save"/>
            </td>
        </tr>
    </table>
</form>
